<template>
  <Header></Header>
  <div class="detail-title">
    <div class="detail-main">
      <div class="detail-breadcrumb">
        <el-breadcrumb :separator-icon="ArrowRight">
          <el-breadcrumb-item>课程</el-breadcrumb-item>
          <el-breadcrumb-item>免费课</el-breadcrumb-item>
          <el-breadcrumb-item>{{ detail.courseName }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="detail-name">{{ detail.courseName }}</div>
      <div class="detail-content">
        难度 入门
      </div>
    </div>
  </div>
  <div class="detail-container">
    <div class="tab-name">
      <ul>
        <li :class="isActive == 1 ? 'active' : ''" @click="isActive = 1">章节</li>
        <li :class="isActive == 2 ? 'active' : ''" @click="isActive = 2">下载笔记代码</li>
      </ul>
    </div>
    <div class="tab-chapters" v-if="isActive == 1">
      <div class="tab-txt">
        <div class="txt-content">
          {{ detail.bizCourseDetail ? detail.bizCourseDetail : '暂无课程介绍...' }}
        </div>
        <div class="txt-btn">
          <div class="payment">立即购买</div>
          <div class="add-cart" @click="addCart">加入购物车</div>
        </div>
      </div>
      <div class="detail-list">
        <div class="item" v-for="item in bizCourseChapters" :key="item.id">
          <div class="item-title">
            <div class="item-name">{{ item.chapterName }}</div>
            <div class="item-sub">{{ item.description }}</div>
          </div>
          <ul>
            <li v-for="item1 in item.children" :key="item1.id">
              <div class="course">
                <img src="@/assets/img/detail-video.png" alt="">
                <div>视频：{{ item1.chapterName }}</div>
              </div>
              <div class="course-video" @click="goPlay(item1)">开始学习</div>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="tab-main" v-if="isActive == 2">
      <ul>
        <li v-for="item in bizCourseAttachments" :key="item.id">
          <div>{{ item.attachmentName }}</div>
          <div class="download" @click="downloadSource(item)">下载资料</div>
        </li>
      </ul>
    </div>
  </div>
  <Footer></Footer>
</template>

<script setup>
import Header from '@/components/common/Header.vue'
import Footer from '@/components/common/Footer.vue'
import { ArrowRight } from '@element-plus/icons-vue'
import { getDetail, courseCheckAuth, downloadAttachment } from '@/utils/api/course.js'
import { addShopCar } from '@/utils/api/cart.js'
import { createToken } from '@/utils/api/createToken.js'
const isActive = ref(1)
const route = useRoute()
const router = useRouter()
const detail = reactive({
  courseName: '',
  bizCourseDetail: ''
})
// 课程章节
const bizCourseChapters = ref([])
// 课程资料
const bizCourseAttachments = ref([])
getDetail({
  courseId: route.query.id
}).then(res => {
  const data = res.data.data
  detail.courseName = data.courseName
  detail.bizCourseDetail = data.bizCourseDetail.description
  bizCourseChapters.value = data.bizCourseChapters
  bizCourseAttachments.value = data.bizCourseAttachments
})
import { useUserStore } from '@/store/user.js'
// 下载资料
const downloadSource = (item) => {
  console.log(item)
  // 判断是否是登录状态，如果没有登录 进入登录页面
  if (!useUserStore().token) {
    ElMessage.info({
      message: '请先登录'
    })
    router.push('/login')
    return
  }
  // 如果是登录状态
  courseCheckAuth({
    courseId: item.courseId,
  }).then(res => {
    // 如果没有权限，提醒购买
    if (!res.data.data.hasAuth) {
      ElMessage.info({
        message: '购买该课程才可以下载资料'
      })
      return
    }
    // 有权限
    downloadAttachment({
      courseId: item.courseId,
      attachmentId: item.id
    }).then(res => {
      console.log(res)
      const blob = new Blob([res])
      let fileName = item.attachmentName
      let fileUrl = item.attachmentUrl
      const extName = fileUrl.substring(fileUrl.lastIndexOf('.'))
      fileName = fileName + extName
      const link = document.createElement('a')
      link.download = fileName
      link.target = '_blank'
      link.style.display = 'none'
      link.href = URL.createObjectURL(blob)
      document.body.appendChild(link)
      link.click()
      URL.revokeObjectURL(link.href)
      document.body.removeChild(link)
    })
  })
}

// 开始学习
const goPlay = (item1) => {
  // 判断是否是登录状态，如果没有登录 进入登录页面
  if (!useUserStore().token) {
    ElMessage.info({
      message: '请先登录'
    })
    router.push('/login')
    return
  }
  // 如果是登录状态
  courseCheckAuth({
    courseId: item1.courseId,
  }).then(res => {
    // 如果没有权限，提醒购买
    if (!res.data.data.hasAuth) {
      ElMessage.info({
        message: '购买该课程才可以下载资料'
      })
      return
    }
    // 有权限
    router.push({
      path: `/course-play/${item1.courseId}/${item1.id}`
    })
  })
}
// 加入购物车
const addCart = () => {
  createToken().then(res => {
    const token = res.data.token
    addShopCar({
      courseId: route.query.id,
      memberId: useUserStore().userInfo.id,
    }, token).then(res => {
      if (res.meta.code === '200') {
        ElMessage.success({
          message: '加入购物车成功~'
        })
      } else {
        ElMessage.info({
          message: '添加失败~该课程已经在购物车中了~'
        })
      }
      console.log(res)
    })
  })
}
</script>

<style scoped lang="scss">
.detail-title {
  width: 100%;
  height: 183px;
  background: url('@/assets/img/detail-bg.png') no-repeat center center / cover;

  .detail-main {
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    width: 1000px;
    height: 100%;
    margin: 0 auto;

    :deep(.el-breadcrumb__separator) {
      color: #fff;
      font-weight: 500;
    }

    :deep(.el-breadcrumb__inner) {
      color: #fff;
      font-weight: 500;
    }

    .detail-name {
      font-size: 27px;
      font-weight: 500;
      color: #fff;
    }

    .detail-content {
      color: #fff;
      font-size: 12px;
      font-weight: 400;
    }
  }
}

.detail-container {
  .tab-name {
    width: 100%;
    height: 57px;
    background: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .16);

    ul {
      width: 1000px;
      margin: 0 auto;
      display: flex;
      align-items: center;
      height: 100%;
      font-size: 16px;
      cursor: pointer;

      li {
        &.active {
          color: #388fff;
          font-weight: bold;
          position: relative;

          &:after {
            content: '';
            position: absolute;
            left: 50%;
            margin-left: -20%;
            bottom: -6px;
            width: 40%;
            height: 3px;
            background: #388fff;
            border-radius: 1px;
          }
        }

        &+li {
          margin-left: 67px;
        }
      }
    }
  }

  .tab-chapters {
    width: 1000px;
    margin: 0 auto;

    .tab-txt {
      margin: 29px auto;
      width: 100%;
      height: 147px;
      background: #fff;
      box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.0900);
      border-radius: 5px;

      .txt-content {
        padding: 24px 27px;
      }

      .txt-btn {
        display: flex;
        justify-content: flex-end;

        .payment {
          width: 99px;
          height: 30px;
          line-height: 30px;
          font-size: 12px;
          text-align: center;
          background: #f11d1d;
          border-radius: 15px;
          color: #fff;
          margin-right: 13px;
        }

        .add-cart {
          width: 99px;
          height: 30px;
          line-height: 30px;
          font-size: 12px;
          text-align: center;
          background: #fde7e7;
          border-radius: 15px;
          color: #f11d1d;
          margin-right: 33px;
        }
      }
    }

    .detail-list {
      margin-top: 30px;

      .item {
        width: 100%;
        background: #fff;
        box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.0900);
        border-radius: 5px;
        margin-bottom: 10px;

        &-title {
          display: flex;
          flex-direction: column;
          padding: 16px 27px;

          .item-name {
            font-size: 16px;
            font-weight: bold;
            color: #333;
          }

          .item-sub {
            margin-top: 10px;
            font-size: 12px;
            font-weight: 400;
            color: #5c5c5c;
          }
        }

        ul {
          li {
            display: flex;
            justify-content: space-between;
            padding: 16px 27px;

            div {
              display: flex;

              &.course {
                align-items: center;
                font-size: 14px;
                font-weight: 400;
                color: #333;

                img {
                  width: 13px;
                  height: 11px;
                  margin-right: 7px;
                }
              }

              &.course-video {
                width: 73px;
                height: 20px;
                font-size: 12px;
                background: #388fff;
                border-radius: 8px;
                cursor: pointer;
                justify-content: center;
                align-items: center;
                color: #fff;
              }
            }
          }
        }
      }
    }
  }

  .tab-main {
    width: 1000px;
    margin: 0 auto;

    ul {
      width: 100%;

      li {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px;
        margin: 10px 0;
        background: #fff;
        border-radius: 8px;
        font-size: 14px;
        line-height: 35px;
        box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.0900);

        .download {
          width: 100px;
          border: none;
          border-radius: 8px;
          margin: 5px 0;
          font-size: 12px;
          text-align: center;
          color: #fff;
          background: #007bff;
          cursor: pointer;
        }
      }
    }
  }
}
</style>